package com.system.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.system.entity.User;
import com.system.service.UserService;

@Controller("userAction")
public class UserAction extends ActionSupport implements ModelDriven<User> {
	@Resource
	private UserService userService;
	private String message;
	private User user = null;

	@Override
	public User getModel() {
		if (user == null) {
			user = new User();
		}
		return user;
	}

	public boolean checkUser() throws IOException {
		User user = (User) ActionContext.getContext().getSession().get("user");
		if (user != null) {
			if (user.getUsername().equals("admin")) {
				return true;
			}
		}
		setMessage("权限不足！！！");
		ServletActionContext.getResponse().setContentType(
				"text/html; charset=UTF-8");
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter printWriter = ServletActionContext.getResponse()
				.getWriter();
		printWriter
				.print("<script type=\"text/javascript\">alert('权限不足！')</script>");
		printWriter.print("<script>window.location.href='error.jsp'</script>");
		return false;
	}

	public String login() {
		User user1 = null;
		user1 = userService.queryByUsername(user.getUsername());
		if (user1 == null) {
			setMessage("用户不存在");
			return LOGIN;
		}
		if (user1.getPassword().equals(user.getPassword())) {
			ActionContext.getContext().getSession().put("user", user);
			return SUCCESS;
		}
		setMessage("密码错误");
		return LOGIN;
	}

	public String register() {
		User user1 = null;
		if (user.getUsername().length() == 0
				|| user.getPassword().length() == 0) {
			setMessage("用户名或者密码都不可以为空");
			return LOGIN;
		}
		user1 = userService.queryByUsername(user.getUsername());
		if (user1 != null) {
			setMessage("该用户名已经被注册");
			return LOGIN;
		}
		String password1 = ServletActionContext.getRequest().getParameter(
				"password1");
		if (!password1.equals(user.getPassword())) {
			setMessage("两次输入的密码不一致");
			return LOGIN;
		}
		userService.add(user);
		ActionContext.getContext().getSession().put("user", user);
		return SUCCESS;
	}

	public String get_user_list() throws IOException {
		if (!checkUser()) {
			return ERROR;
		}
		List<User> users = null;
		users = userService.queryAll();
		ActionContext.getContext().getSession().put("users", users);
		return SUCCESS;
	}

	public String user_delete() throws IOException {
		if (!checkUser()) {
			return ERROR;
		}
		int user_id = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("user_id"));
		user = userService.queryById(user_id);
		if (user.getUsername().equals("admin")) {
			setMessage("不可以删除此用户");
			return INPUT;
		}
		userService.delete(user);
		List<User> users = null;
		users = userService.queryAll();
		ActionContext.getContext().getSession().put("users", users);
		return SUCCESS;
	}

	public String user_add() throws IOException {
		if (!checkUser()) {
			return ERROR;
		}
		if (user.getUsername().length() == 0
				|| user.getPassword().length() == 0) {
			setMessage("用户名或密码不可以为空");
			return INPUT;
		}
		if (userService.queryByUsername(user.getUsername()) != null) {
			setMessage("用户已经存在");
			return INPUT;
		}
		String password1 = ServletActionContext.getRequest().getParameter(
				"password1");
		if (!password1.equals(user.getPassword())) {
			setMessage("两次输入的密码不一致");
			return INPUT;
		}
		userService.add(user);
		get_user_list();
		return SUCCESS;
	}

	public String user_modify() throws IOException {
		if (!checkUser()) {
			return ERROR;
		}
		user = userService.queryById(user.getUser_id());
		ActionContext.getContext().getSession().put("user", user);
		return SUCCESS;
	}

	public String user_update() throws IOException {
		if (!checkUser()) {
			return ERROR;
		}
		if (user.getPassword().length() == 0) {
			setMessage("密码不可以为空");
			return INPUT;
		}
		userService.update(user);
		return SUCCESS;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
